/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import br.ufmg.dees.insane.draw.Draw;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 * This class provides a tabbed pane for the dialog.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.Interface
 * @since jdk1.4.1
 */
public abstract class CreateDialog extends JComponent implements ActionListener{
	
    public static final int OK_OPTION = 0;
    public static final int CANCEL_OPTION = 1;
	
    private JDialog dlg;
    private int command;
    private JPanel confirmationPane = new JPanel();
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("Cancel");
    protected JPanel mainPane = new JPanel();
    protected MyCanvas myCanvas = new MyCanvas();
	
//*****************************************************************************
	
    public CreateDialog(JFrame parent, String title){
        dlg = new JDialog(parent, title, true);
        dlg.setLocationRelativeTo(parent);
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dlg.setResizable(false);
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(mainPane, BorderLayout.CENTER);
        dlg.getContentPane().add(confirmationPane, BorderLayout.SOUTH);
        dlg.getRootPane().setDefaultButton(ok);
		
        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
        JPanel canvasPane = new JPanel();
        canvasPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createEmptyBorder(2,2,2,2),
        BorderFactory.createLoweredBevelBorder()));
        canvasPane.add(myCanvas);
        mainPane.add(canvasPane);
		
        //Sets up confirmation pane
        confirmationPane.add(ok);
        ok.addActionListener(this);
        cancel.setMnemonic(KeyEvent.VK_C);
        confirmationPane.add(cancel);
        cancel.addActionListener(this);
    }
	
//*****************************************************************************
	
    /**
     * Constructor method
     *
     * @see gui.Interface
     */
    public int showDialog() {
        dlg.pack();
        dlg.setVisible(true);
        return command;
    }
	
//*****************************************************************************
	
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        if (btn.getActionCommand() == "OK"){
            command = OK_OPTION;
            approvedPerform();
        }else{
            command = CANCEL_OPTION;
        }
        dlg.setVisible(false);
    }
	
//*****************************************************************************
	
    /**
     * Method executed on OK button click.
     */
    public Draw approvedPerform() {
        return myCanvas.getDraw();
    }
	
//*****************************************************************************
}

//*****************************************************************************
//*****************************************************************************

class MyCanvas extends Canvas {
	
	private static final long serialVersionUID = 1L;
	private Draw draw;
    private Rectangle drawHere;
	
//*****************************************************************************
	
    public MyCanvas() {
        this.setSize(new Dimension(200, 150));
    }
	
//*****************************************************************************
	
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        drawHere = g2.getClipBounds();
        g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        qualityHints.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHints(qualityHints);
        g2.setColor(Color.white);
        g2.fill(drawHere);
        g2.setColor(Color.black);
        g2.setFont(new Font("Arial", Font.PLAIN, 10));
        Point c = this.getCenter();
        Dimension d = this.getSize();
        g2.fill(new Ellipse2D.Double(c.x, c.y, 3, 3));
        g2.draw(new Line2D.Double(c.x, 5, c.x, d.height));
        g2.draw(new Line2D.Double(c.x, 5, c.x - 5, 10));
        g2.draw(new Line2D.Double(c.x, 5, c.x + 5, 10));
        g2.drawString("y", c.x - 10, 20);
        g2.draw(new Line2D.Double(0, c.y, d.width - 5, c.y));
        g2.draw(new Line2D.Double(d.width - 5, c.y, d.width - 10, c.y + 5));
        g2.draw(new Line2D.Double(d.width - 5, c.y, d.width - 10, c.y - 5));
        g2.drawString("x", d.width - 20, c.y + 15);
        g2.draw(new Arc2D.Double(d.width - 100, c.y - 40, 80, 80, 0, 75, Arc2D.OPEN));
        g2.draw(new Line2D.Double(d.width - 50, c.y - 39, d.width - 45, c.y - 41));
        g2.draw(new Line2D.Double(d.width - 50, c.y - 39, d.width - 47, c.y - 34));
        g2.drawString("theta", d.width - 35, c.y - 35);
        draw.setLocation(c);
        draw.draw(g2);
    }
	
//*****************************************************************************
	
    public void setDraw(Draw d) {
        draw = d;
    }
	
//*****************************************************************************
	
    public Draw getDraw() {
        return draw;
    }
	
//*****************************************************************************
	
    public Point getCenter() {
        Point p = new Point((int)(drawHere.width/2), (int)(drawHere.height/2));
        return p;
    }
	
//*****************************************************************************
	
    public Dimension getSize() {
        return super.getSize();
    }
	
//*****************************************************************************
}
